package com.jy.myhome.service;

import com.jy.myhome.dao.UserDao;
import com.jy.myhome.entity.Users;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserDao ud;

    //根据id查询用户信息
    @Override
    public Users findUserOne(Integer id) {
        return ud.findUserOne(id);
    }


    //登陆
    @Override
    public Map login(Users user, HttpSession session) {

        Users u= ud.login(user.getPhone());
        int status = 0;//存储状态
        if (u == null) {//手机号不存在
            status = -1;
        } else {
            //System.out.print(u.getPassword()+"asdasd");
            if (!user.getPassword().equals(u.getPassword())) {//密码错误
                status = 0;
                u = null;
            } else {
                //用户名和密码正确
                //把user存入session
                session.setAttribute("user", u);
                status = 1;
            }
        }

        //保存实体和状态给前端
        Map m = new HashMap();
        m.put("status", status);
        m.put("user", u);
        return m;
    }


    //注册(添加用户)
    @Override
    public Integer register(Users user){
        try {
            return ud.addUser(user);
        }catch (Exception e){//用户名已存在
            return 0;
        }

    }

    //修改用户信息
    @Override
    public Integer updateUser(Users user) {
        return ud.updateUser(user);
    }


}
